import uuid,os
from django.db import models
from django.utils import timezone
from op_drf.base import BaseModel
# Create your models here.


class Website(BaseModel):
    '''
    接口信息
    '''
    name = models.CharField(max_length=255,verbose_name="公司名称",help_text="公司名称",null=False)
    hsptcd = models.CharField(max_length=20,verbose_name="医院编码",help_text="医院编码",null=True)
    # 同步数据时Rad连接信息
    ftp_ip = models.CharField(max_length=20, verbose_name="ftpip", help_text="ftpip", null=True)
    ftp_port = models.IntegerField(verbose_name="ftp端口", help_text="ft端口", null=True)
    ftp_user = models.CharField(max_length=20, verbose_name="ftp用户名", help_text="ftp用户名", null=True)
    ftp_password = models.CharField(max_length=50, verbose_name="ftp密码", help_text="ftp密码", null=True)
    end_datetime = models.DateTimeField(verbose_name="到期时间", help_text="到期时间", null=True)
    deltadays = models.IntegerField(verbose_name="天数",help_text="天数",null=False,default=0)


    class Meta:
        db_table = "atm_website"
        verbose_name = "接口信息"
        verbose_name_plural = verbose_name

class User(models.Model):
    username = models.CharField(max_length=20,verbose_name="用户名",help_text="用户名",null=False)
    pwd = models.CharField(max_length=100,verbose_name="密码",help_text="密码",null=True)
    is_delete = models.BooleanField(null=False,default=False,verbose_name="删除标记",help_text="删除标记")

    class Meta:
        db_table = "atm_user"
        verbose_name = "用户"
        verbose_name_plural = verbose_name


def files_path(instance, filename):
    return "/".join(
        [
            "system",
            timezone.now().strftime("%Y-%m-%d"),
            str(uuid.uuid4()) + os.path.splitext(filename)[-1],
        ]
    )


class SaveFile(BaseModel):
    name = models.TextField(verbose_name="文件名称", null=True, blank=True)
    type = models.CharField(max_length=200, verbose_name="文件类型", null=True, blank=True)
    size = models.CharField(max_length=64, verbose_name="文件大小", null=True, blank=True)
    address = models.CharField(max_length=16, verbose_name="存储位置", null=True, blank=True)  # 本地、阿里云、腾讯云..
    source = models.CharField(max_length=16, verbose_name="文件来源", null=True, blank=True)  # 导出、用户上传.
    oss_url = models.CharField(max_length=200, verbose_name="OSS地址", null=True, blank=True)
    status = models.BooleanField(default=True, verbose_name="文件是否存在")
    file = models.FileField(
        verbose_name="文件URL",
        upload_to=files_path,
    )
    model = models.CharField(max_length=100, null=True, verbose_name="关联模型")
    relation_id = models.IntegerField(null=True, verbose_name="关联字段")
    model_type = models.CharField(max_length=100, null=True, verbose_name="类型")
    info = models.TextField(verbose_name="文件信息", null=True, blank=True)

    class Meta:
        db_table = "atm_save_file"
        verbose_name = "文件管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.name}"


